package com.example.project.controller;



import com.example.project.pojo.Result;
import com.example.project.pojo.User;
import com.example.project.service.UserService;
import com.example.project.utils.JwtUtils;
import com.example.project.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
//@Validated   //validation spring 提供的校验数据工具  @Pattern(regexp="^\\${}$")
public class UserController {
    @Autowired
    private UserService userService;
   //添加新管理员
    @PostMapping("/register")
    public Result register(String username, String password,String name){
        //查询管理员
            User u = userService.findbyusername(username);
            if (u == null) {
                userService.register(username, password,name);
                return  Result.success();

            } else {
               return Result.error("用户名已被占用");
            }
        }
        //获取用户信息
        @GetMapping ("/userInfo")
        public Result<User> userinfo(){

            Map<String,Object> map= ThreadLocalUtil.get();
            String username =(String) map.get("username");
            User u=userService.findbyusername(username);
            return Result.success(u);
        }


    //登录
    @PostMapping("/login")
    public  Result<String>loginIn(String username, String password){

            User u = userService.findbyusername(username);
            if(u==null){

                return  Result.error("用户名错误");
            }

            if(password.equals(u.getPassword())){
                Map<String,Object> claims=new HashMap<>();
                claims.put("id",u.getId());
                claims.put("username",u.getUsername());
                claims.put("name",u.getName());
                String token = JwtUtils.genToken(claims);

                return Result.success(token);
            }


                return Result.error("用户名或密码错误");

    }

    //更新密码
    @PatchMapping("/updatePwd")
    public  Result updatePwd(@RequestBody Map<String,String> params){
        //校验参数
       String oldpwd=params.get("old_pwd");
       String newpwd=params.get("new_pwd");
       String repwd=params.get("re_pwd");

        if(!StringUtils.hasLength(oldpwd)||!StringUtils.hasLength(newpwd)||!StringUtils.hasLength(repwd)){
            return Result.error("缺少必要参数");
        }

        //原密码是否正确
        Map<String,Object> map=ThreadLocalUtil.get();
        User loginUser=userService.findbyusername((String) map.get("username"));
        if(!loginUser.getPassword().equals(oldpwd)){
            return Result.error("原密码不正确");
        }
        if(!newpwd.equals(repwd)){
            return Result.error("两次填写的新密码不一样");
        }
        userService.updatePwd(newpwd);
        return Result.success();

    }

}
